package cn.newzai.parser.proto.result.generate

import cn.newzai.parser.proto.result._
import GenerateCodeType._


/**
 * Created with IntelliJ IDEA.
 * User: chenxiaohong
 * Date: 13-6-21
 * Time: 上午9:32
 * To change this template use File | Settings | File Templates.
 */
trait GenerateCSharpConvert2String extends EnumGenerate{
  this : Enum =>
  override def generate_code(gen_type: Value): String = {
    if( gen_type == CSharpEnumValue2String){
      def eachValueConvert( eName :String) ={
        s"""    if( evalue == ${Message.package_name}.${name}.${eName} ) { return "${eName}";} """
      }
      def eachValueBack(eName :String) ={
        s"""    if( item == "${eName}" ) { return ${Message.package_name}.${name}.${eName};}"""
      }
      s"""
        |public class ${name}2String : IValueConverter
        |{
        | public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        | {
        |   ${Message.package_name}.${name} evalue = (${Message.package_name}.${name})value;
        |   ${ indexes.map( v => eachValueConvert(v._1)).mkString("\n")}
        |   return "${default}";
        | }
        | public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        | {
        |   string item = (string)(value);
        |   ${indexes.map( v => eachValueBack( v._1)).mkString("\n")}
        |   return    ${Message.package_name}.${name}.${default};
        | }
        |}
      """.stripMargin
    }else{
      super.generate_code(gen_type)
    }
  }
}


trait GenerateCSharpConvert2Index extends EnumGenerate{
  this : Enum =>
  override def generate_code(gen_type: Value): String = {
    if( gen_type == CSharpEnumValue2Index){

      def eachValueConvert( index : Int,  eName :String) ={
        s"    if( evalue == ${Message.package_name}.${name}.${eName} ) { return ${index};}"
      }
      def eachValueBack( index : Int , eName :String) ={
        s"    if( index == ${index} ) { return ${Message.package_name}.${name}.${eName};}"
      }
      s"""
        |public class ${name}2Index : IValueConverter
        |{
        | public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        | {
        |   ${Message.package_name}.${name} evalue = (${Message.package_name}.${name})value;
        |   ${ indexes.map( v => eachValueConvert(v._2,v._1)).mkString("\n")}
        |   return 0;
        | }
        | public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        | {
        |   int index = (int)(value);
        |   ${indexes.map( v => eachValueBack(v._2, v._1)).mkString("\n")}
        |   return    ${Message.package_name}.${name}.${default};
        | }
        |}
      """.stripMargin
    }else{
      super.generate_code(gen_type)
    }
  }
}


trait GenerateCSharpEnumResource extends EnumGenerate{
  this : Enum =>
  override def generate_code(gen_type: Value): String = {

    if( gen_type == CSharpEnumResource){
      s"""
      |<local:${name}2Index x:Key="${name.toLowerCase}2Index"/>
      |<local:${name}2String x:Key="${name.toLowerCase}2String"/>""".stripMargin
    }else {
      super.generate_code(gen_type)
    }
  }
}

trait GenerateCSharpEnumCombox extends EnumGenerate{
  this : Enum =>

  override def generate_code(gen_type: Value): String = {

    def eachItem( itemName : String ) =
    {
      s"  <ComboBoxItem>${itemName}</ComboBoxItem>"
    }
    if( gen_type == CSharpEnumCombox){
      s"""
        |<ComboBox  SelectedIndex="{Binding Path=%BindPath%,Converter={StaticResource ${name.toLowerCase}2Index },Mode=TwoWay}">
        |${values.map( v => eachItem(v._1)).mkString("\n")}
        |</ComboBox>
      """.stripMargin
    }else {
      super.generate_code(gen_type)
    }
  }
}

trait  GenerateCSharpEnumRadioGroup extends EnumGenerate
{
  this : Enum =>
  override def generate_code(gen_type: Value): String = {
    if(gen_type == CSharpEnumRadioGroup){

      def eachValue( value : String) ={
        s"""<RadioButton Margin="5,0,5,0"  GroupName="${name}" Content="${value}" IsChecked="{Binding Path=${value},Mode=TwoWay}"></RadioButton>"""
      }

      s"""
        | <StackPanel DataContext="{Binding Path=%bindpath%}"  Margin="5,0,5,0" Orientation="Horizontal" >
        |${ values.map( v => eachValue(v._1)).mkString("\n")}
        | </StackPanel>
      """.stripMargin
    }else{
      super.generate_code(gen_type)
    }
  }
}